package cn.careerx.app.common.models;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.elasticsearch.action.index.IndexRequestBuilder;
import org.elasticsearch.action.index.IndexResponse;
import org.elasticsearch.client.Client;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.alibaba.fastjson.JSONObject;
import com.avaje.ebean.EbeanServer;
import com.avaje.ebean.SqlQuery;
import com.avaje.ebean.SqlRow;
import com.avaje.ebean.SqlUpdate;
import com.baidu.bjf.remoting.protobuf.Codec;
import com.baidu.bjf.remoting.protobuf.FieldType;
import com.baidu.bjf.remoting.protobuf.ProtobufProxy;
import com.baidu.bjf.remoting.protobuf.annotation.Protobuf;


import redis.clients.jedis.Jedis;
import redis.clients.util.SafeEncoder;

public class League implements Serializable{

	private static final long serialVersionUID = 3684710315016649166L;
	
	private static final Logger logger = LoggerFactory.getLogger(League.class);
	
	@Protobuf(fieldType=FieldType.STRING, order=1, required=false)
	private String id;
	
	@Protobuf(fieldType=FieldType.STRING, order=2, required=false)
	private String name;
	
	@Protobuf(fieldType=FieldType.STRING, order=3, required=false)
	private String abbr;
	
	@Protobuf(fieldType=FieldType.STRING, order=4, required=false)
	private String logoKey;
	
	@Protobuf(fieldType=FieldType.INT32, order=5, required=false)
	private Integer locationId; 
	
	@Protobuf(fieldType=FieldType.STRING, order=6, required=false)
	private String address;
	
	@Protobuf(fieldType=FieldType.STRING, order=7, required=false)
	private String schoolId;
	
	@Protobuf(fieldType=FieldType.STRING, order=8, required=false)
	private String schoolName;
	
	@Protobuf(fieldType=FieldType.INT32, order=9, required=false)
	private Integer industry;
	
	@Protobuf(fieldType=FieldType.INT32, order=10, required=false)
	private Integer size;
	
	@Protobuf(fieldType=FieldType.STRING, order=11, required=false)
	private String zipCode; 
	
	@Protobuf(fieldType=FieldType.STRING, order=12, required=false)
	private String tel;
	
	@Protobuf(fieldType=FieldType.STRING, order=13, required=false)
	private String fax;
	
	@Protobuf(fieldType=FieldType.STRING, order=14, required=false)
	private String email;
	
	@Protobuf(fieldType=FieldType.STRING, order=15, required=false)
	private String contact;
	
	@Protobuf(fieldType=FieldType.INT32, order=16, required=false)
	private Integer postCount;
	
	@Protobuf(fieldType=FieldType.STRING, order=17, required=false)
	private String wxUrl;
	
	@Protobuf(fieldType=FieldType.INT64, order=18, required=false)
	private Long sortValue;
	
	@Protobuf(fieldType=FieldType.INT32, order=19, required=false)
	private Integer isValid; 
	
	@Protobuf(fieldType=FieldType.STRING, order=20, required=false)
	private String descr;
	
	@Protobuf(fieldType=FieldType.INT64, order=21, required=false)
	private Long createAt;
	
	@Protobuf(fieldType=FieldType.INT64, order=22, required=false)
	private Long updateAt;
	
	public static League create(){
		return new League();
	}

	public String getId() {
		return id;
	}

	public League setId(String id) {
		this.id = id;
		return this;
	}

	public String getName() {
		return name;
	}

	public League setName(String name) {
		this.name = name;
		return this;
	}

	public String getAbbr() {
		return abbr;
	}

	public League setAbbr(String abbr) {
		this.abbr = abbr;
		return this;
	}

	public String getLogoKey() {
		return logoKey;
	}

	public League setLogoKey(String logoKey) {
		this.logoKey = logoKey;
		return this;
	}

	public Integer getLocationId() {
		return locationId;
	}

	public League setLocationId(Integer locationId) {
		this.locationId = locationId;
		return this;
	}

	public String getAddress() {
		return address;
	}

	public League setAddress(String address) {
		this.address = address;
		return this;
	}

	public String getSchoolId() {
		return schoolId;
	}

	public League setSchoolId(String schoolId) {
		this.schoolId = schoolId;
		return this;
	}

	public String getSchoolName() {
		return schoolName;
	}

	public League setSchoolName(String schoolName) {
		this.schoolName = schoolName;
		return this;
	}

	public Integer getIndustry() {
		return industry;
	}

	public League setIndustry(Integer industry) {
		this.industry = industry;
		return this;
	}

	public Integer getSize() {
		return size;
	}

	public League setSize(Integer size) {
		this.size = size;
		return this;
	}

	public String getZipCode() {
		return zipCode;
	}

	public League setZipCode(String zipCode) {
		this.zipCode = zipCode;
		return this;
	}

	public String getTel() {
		return tel;
	}

	public League setTel(String tel) {
		this.tel = tel;
		return this;
	}

	public String getFax() {
		return fax;
	}

	public League setFax(String fax) {
		this.fax = fax;
		return this;
	}

	public String getEmail() {
		return email;
	}

	public League setEmail(String email) {
		this.email = email;
		return this;
	}

	public String getContact() {
		return contact;
	}

	public League setContact(String contact) {
		this.contact = contact;
		return this;
	}

	public Integer getPostCount() {
		return postCount;
	}

	public League setPostCount(Integer postCount) {
		this.postCount = postCount;
		return this;
	}

	public String getWxUrl() {
		return wxUrl;
	}

	public League setWxUrl(String wxUrl) {
		this.wxUrl = wxUrl;
		return this;
	}

	public Long getSortValue() {
		return sortValue;
	}

	public League setSortValue(Long sortValue) {
		this.sortValue = sortValue;
		return this;
	}

	public Integer getIsValid() {
		return isValid;
	}

	public League setIsValid(Integer isValid) {
		this.isValid = isValid;
		return this;
	}

	public String getDescr() {
		return descr;
	}

	public League setDescr(String descr) {
		this.descr = descr;
		return this;
	}

	public Long getCreateAt() {
		return createAt;
	}

	public League setCreateAt(Long createAt) {
		this.createAt = createAt;
		return this;
	}

	public Long getUpdateAt() {
		return updateAt;
	}

	public League setUpdateAt(Long updateAt) {
		this.updateAt = updateAt;
		return this;
	}

	// 添加Redis
	public boolean toRedis(Jedis jedis) {
		if (id == null) {
			return false;
		}
		try {
			String key = "league:" + id;
			byte[] bKey = SafeEncoder.encode(key);
			Codec<League> codec = ProtobufProxy.create(League.class);
			jedis.set(bKey, codec.encode(this));
			return true;
		} catch (Exception e) {
			logger.error("Error persist League to toRedis : ", e);
		}
		return false;
	}
	
	//添加索引
	public boolean toEs(Client client){
		try {
			IndexRequestBuilder rb = client.prepareIndex().setIndex("cxorg").setType("org").setId(id);
			JSONObject source = new JSONObject();
			source.put("name", this.getName());
			source.put("abbr", this.getAbbr());
			source.put("schoolId", this.getSchoolId());
			source.put("schoolName", this.getSchoolName());
			source.put("status", this.getIsValid());
			source.put("type", 4);
			source.put("updateAt", this.getUpdateAt());
			
			rb.setSource(source.toJSONString());
			IndexResponse resp = rb.execute().actionGet();
			return resp.isCreated();
		} catch (Exception e) {
			logger.error("Error index League to es, id = "+id, e);
			return false;
		}
	}

	// 从Redis删除
	public static boolean deleteRedis(Jedis jedis, League league) {
		if (league.getId() == null) {
			return false;
		}
		try {
			String key = "league:" + league.getId();
			byte[] bKey = SafeEncoder.encode(key);
			if (jedis.exists(bKey)) {
				jedis.del(bKey);
			}
			return true;
		} catch (Exception e) {
			logger.error("Error persist League to deleteRedis : ", e);
		}
		return false;
	}

	// Redis:根据ID获取League
	public static League findFromRedisById(Jedis jedis, String id) {
		League league = null;
		String key = "league:" + id;
		byte[] bKey = SafeEncoder.encode(key);
		Codec<League> codec = ProtobufProxy.create(League.class);
		byte[] data = jedis.get(bKey);
		if (data != null) {
			try {
				league = codec.decode(data);
			} catch (Exception ignore) {
				jedis.del(bKey);
			}
		}
		return league;
	}
	
	// 把SqlRow类型转化成League
	public static League fromDbRow(SqlRow sqlRow) {
		League result = League.create()
				.setId(sqlRow.getString("ID"))
				.setName(sqlRow.getString("NAME"))
				.setAbbr(sqlRow.getString("ABBR"))
				.setLocationId(sqlRow.getInteger("LOCATION_ID"))
				.setAddress(sqlRow.getString("ADDRESS"))
				.setSchoolId(sqlRow.getString("SCHOOL_ID"))
				.setSchoolName(sqlRow.getString("SCHOOL_NAME"))
				.setSize(sqlRow.getInteger("SIZE"))
				.setIndustry(sqlRow.getInteger("INDUSTRY"))
				.setLogoKey(sqlRow.getString("LOGO_KEY"))
				.setZipCode(sqlRow.getString("ZIPCODE"))
				.setTel(sqlRow.getString("TEL"))
				.setFax(sqlRow.getString("FAX"))
				.setEmail(sqlRow.getString("EMAIL"))
				.setContact(sqlRow.getString("CONTACT"))
				.setPostCount(sqlRow.getInteger("POST_COUNT"))
				.setSortValue(sqlRow.getLong("SORT_VALUE"))
				.setDescr(sqlRow.getString("DESCR"))
				.setWxUrl(sqlRow.getString("WX_URL"))
				.setIsValid(sqlRow.getInteger("IS_VALID"))
				.setCreateAt(sqlRow.getLong("CREATE_AT"))
				.setUpdateAt(sqlRow.getLong("UPDATE_AT"));
		return result;
	}
		
	// DB:根据ID获取League
	public static League findFromDBById(Database db, Jedis jedis, String id) {
		League league = null;
		EbeanServer es = db.getDbByPartitionKey("CAREERX", "");
		StringBuffer sqlStr = new StringBuffer();
		sqlStr.append("SELECT * FROM CX_LEAGUE WHERE IS_VALID<>-1 AND ID=:id");
		SqlQuery sqlQuery = es.createSqlQuery(sqlStr.toString()).setParameter("id", id);
		try {
			SqlRow sqlRow = sqlQuery.findUnique();
			if (sqlRow == null || sqlRow.size()==0) {
				return league;
			}
			league = fromDbRow(sqlRow);
			league.toRedis(jedis);
		} catch (Exception e) {
			logger.error("Error persist League to findFromDBById : ", e);
		}
		return league;
	}
	
	//DB:根据ID获取League
	public static League findAllFromDBById(Database db, String id) {
		League league = null;
		EbeanServer es = db.getDbByPartitionKey("CAREERX", "");
		StringBuffer sqlStr = new StringBuffer();
		sqlStr.append("SELECT * FROM CX_LEAGUE WHERE ID=:id");
		SqlQuery sqlQuery = es.createSqlQuery(sqlStr.toString()).setParameter("id", id);
		try {
			SqlRow sqlRow = sqlQuery.findUnique();
			if (sqlRow == null || sqlRow.size()==0) {
				return league;
			}
			league = fromDbRow(sqlRow);
		} catch (Exception e) {
			logger.error("Error persist League to findAllFromDBById : ", e);
		}
		return league;
	}
	
	// DB:根据ID从数据库获取SchoolId
	public static String findSchoolIdFromDBById(Database db, Jedis jedis, String id) {
		String schoolId = null;
		EbeanServer es = db.getDbByPartitionKey("CAREERX", "");
		StringBuffer sqlStr = new StringBuffer();
		sqlStr.append("SELECT * FROM CX_LEAGUE WHERE IS_VALID<>-1 AND ID=:id");
		SqlQuery sqlQuery = es.createSqlQuery(sqlStr.toString()).setParameter("id", id);
		try {
			SqlRow sqlRow = sqlQuery.findUnique();
			if (sqlRow == null || sqlRow.size()==0) {
				return schoolId;
			}
			schoolId = sqlRow.getString("SCHOOL_ID");
		} catch (Exception e) {
			logger.error("Error persist League to findSchoolIdFromDBById : ", e);
		}
		return schoolId;
	}

	// 获得League
	public static League findFromRedisWithDbById(Database db, Jedis jedis, String id) {
		League league = findFromRedisById(jedis, id);
		if (league == null) {
			league = findFromDBById(db, jedis, id);
		}
		return league;
	}
	
	// 获得League
	public static League findAllFromRedisWithDbById(Database db, Jedis jedis, String id) {
		League league = findFromRedisById(jedis, id);
		if (league == null) {
			league = findAllFromDBById(db, id);
		}
		return league;
	}
	
	//批量获取League
	public static ArrayList<League> getLeagueByIds( Database db,Jedis jedis,String[] leagueIds ){
		ArrayList<League> result = null;
		if (leagueIds!=null && leagueIds.length>0) {
			result = new ArrayList<League>();
			for( String id : leagueIds ){
				League league = findFromRedisWithDbById(db, jedis, id);
				if (league != null) {
					result.add(league);
				}
			}
		}
		return result;
	}
	
	//批量获取League
	public static ArrayList<League> getAllLeagueByIds( Database db,Jedis jedis,String[] leagueIds ){
		ArrayList<League> result = null;
		if (leagueIds!=null && leagueIds.length>0) {
			result = new ArrayList<League>();
			for( String id : leagueIds ){
				League league = findAllFromRedisWithDbById(db, jedis, id);
				if (league != null) {
					result.add(league);
				}
			}
		}
		return result;
	}
	
	
	//通过学校ID获取社团ID列表
//	public static ArrayList<String> getLeagueIdsBySchoolId( Database db, String schoolId ){
//		ArrayList<String> leagueIdList = new ArrayList<String>();
//		EbeanServer es = db.getDbByPartitionKey("CAREERX", "");
//		StringBuffer sqlStr = new StringBuffer();
//		sqlStr.append("SELECT DISTINCT(ID) FROM CX_LEAGUE WHERE IS_VALID=1 AND (SCHOOL_ID=:schoolId OR SCHOOL_ID IS NULL )");
//		SqlQuery sqlQuery = es.createSqlQuery(sqlStr.toString())
//				.setParameter("schoolId", schoolId);
//		try{
//			List<SqlRow> sqlRow = sqlQuery.findList();
//			for( SqlRow item : sqlRow ){
//				String leagueId = item.getString("ID");
//				leagueIdList.add(leagueId);
//			}
//			return leagueIdList;
//		}catch( Exception e ){
//			logger.error("Error persist League to getLeagueIdListBySchoolId : ", e);
//		}
//		return null;
//	}
	
	//通过学校ID获取社团ID字符串
	public static String getLeagueIdsBySchoolId( Database db, String schoolId ){
		StringBuffer result = new StringBuffer();
		EbeanServer es = db.getDbByPartitionKey("CAREERX", "");
		StringBuffer sqlStr = new StringBuffer();
		sqlStr.append("SELECT DISTINCT(ID) FROM CX_LEAGUE WHERE IS_VALID=1 AND SCHOOL_ID=:schoolId");
		SqlQuery sqlQuery = es.createSqlQuery(sqlStr.toString())
				.setParameter("schoolId", schoolId);
		try{
			List<SqlRow> sqlRow = sqlQuery.findList();
			if (sqlRow != null && sqlRow.size()>0) {
				for( SqlRow item : sqlRow ){
					String leagueId = item.getString("ID");
					result.append("'"+leagueId+"',");
				}
				return result.toString().substring(0, result.toString().length()-1);
			}
		}catch( Exception e ){
			logger.error("Error persist League to getLeagueIdsBySchoolId : ", e);
		}
		return null;
	}
	
	public static String getLeagueIdsBySchoolId( Database db){
		StringBuffer result = new StringBuffer();
		EbeanServer es = db.getDbByPartitionKey("CAREERX", "");
		StringBuffer sqlStr = new StringBuffer();
		sqlStr.append("SELECT DISTINCT(ID) FROM CX_LEAGUE WHERE IS_VALID=1 AND (SCHOOL_ID IS NULL OR SCHOOL_ID='')");
		SqlQuery sqlQuery = es.createSqlQuery(sqlStr.toString());
		try{
			List<SqlRow> sqlRow = sqlQuery.findList();
			if (sqlRow != null && sqlRow.size()>0) {
				for( SqlRow item : sqlRow ){
					String leagueId = item.getString("ID");
					result.append("'"+leagueId+"',");
				}
				return result.toString().substring(0, result.toString().length()-1);
			}
		}catch( Exception e ){
			logger.error("Error persist League to getLeagueIdsBySchoolId : ", e);
		}
		return null;
	}
	
//	//根据checkName模糊查询leagueId
//	public static List<String> getLeagueIdByCheckName(Database db, Jedis jedis, String checkName){
//		EbeanServer es = db.getDbByPartitionKey("CAREERX", "");
//		StringBuffer sqlStr = new StringBuffer();
//		sqlStr.append("SELECT * FROM CX_LEAGUE WHERE IS_VALID<>-1");
//		sqlStr.append(" AND NAME LIKE :name");
//		SqlQuery sqlQuery = es.createSqlQuery(sqlStr.toString())
//				.setParameter("name", "%"+checkName+"%");
//		try{
//			List<SqlRow> sqlRow = sqlQuery.findList();
//			if (sqlRow==null || sqlRow.size()==0) {
//				return null;
//			}
//			List<String> result = new ArrayList<String>();
//			for( SqlRow sqlItem : sqlRow ){
//				result.add(sqlItem.getString("ID"));
//			}
//			return result;
//		}catch( Exception e ){
//			logger.error("Error persist League to getLeagueIdByCheckName : ", e);
//		}
//		return null;
//	}
	
	//判断该名称是否存在
	public static int checkName(Database db, Jedis jedis, String id, String name){
		int result = 0;
		EbeanServer es = db.getDbByPartitionKey("CAREERX", "");
		StringBuffer sqlStr = new StringBuffer();
		sqlStr.append("SELECT * FROM CX_LEAGUE WHERE IS_VALID<>-1 AND NAME=:name");
		if (id!=null && !id.equals("")) {
			sqlStr.append(" AND ID<>:id");
		}
		SqlQuery sqlQuery = es.createSqlQuery(sqlStr.toString())
				.setParameter("name", name)
				.setParameter("id", id);
		try {
			List<SqlRow> sqlRow = sqlQuery.findList();
			if (sqlRow==null || sqlRow.size()==0) {
				result = 0;
			}
			else{
				result = 1;
			}
		} catch (Exception e) {
			logger.error("Error persist Org to checkName : ", e);
		}
		return result;
	}
	
	//判断该简称是否存在
	public static int checkAbbr(Database db, Jedis jedis, String id, String abbr){
		int result = 0;
		EbeanServer es = db.getDbByPartitionKey("CAREERX", "");
		StringBuffer sqlStr = new StringBuffer();
		sqlStr.append("SELECT * FROM CX_LEAGUE WHERE IS_VALID<>-1 AND ABBR=:abbr");
		if (id!=null && !id.equals("")) {
			sqlStr.append(" AND ID<>:id");
		}
		SqlQuery sqlQuery = es.createSqlQuery(sqlStr.toString())
				.setParameter("abbr", abbr)
				.setParameter("id", id);
		try {
			List<SqlRow> sqlRow = sqlQuery.findList();
			if (sqlRow==null || sqlRow.size()==0) {
				result = 0;
			}
			else{
				result = 1;
			}
		} catch (Exception e) {
			logger.error("Error persist Org to checkAbbr : ", e);
		}
		return result;
	}
	
	//创建League
	public static int toDb( Database db, Jedis jedis, Client client, League league, Date currTime ){
		EbeanServer es = db.getDbByPartitionKey("CAREERX", "");
		StringBuffer sqlStr = new StringBuffer();
		sqlStr.append("INSERT INTO CX_LEAGUE(ID,NAME,ABBR,LOGO_KEY,LOCATION_ID,ADDRESS,SCHOOL_ID,SCHOOL_NAME,INDUSTRY,SIZE,ZIPCODE,TEL,FAX,EMAIL,CONTACT,POST_COUNT,WX_URL,SORT_VALUE,IS_VALID,DESCR,CREATE_AT,UPDATE_AT)"+
				" VALUES ( ?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,? )");
		SqlUpdate sqlUpdate = es.createSqlUpdate(sqlStr.toString())
		.setParameter(1, league.getId())
		.setParameter(2, league.getName())
		.setParameter(3, league.getAbbr())
		.setParameter(4, league.getLogoKey())
		.setParameter(5, league.getLocationId())
		.setParameter(6, league.getAddress())
		.setParameter(7, league.getSchoolId())
		.setParameter(8, league.getSchoolName())
		.setParameter(9, league.getIndustry())
		.setParameter(10, league.getSize())
		.setParameter(11, league.getZipCode())
		.setParameter(12, league.getTel())
		.setParameter(13, league.getFax())
		.setParameter(14, league.getEmail())
		.setParameter(15, league.getContact())
		.setParameter(16, league.getPostCount())
		.setParameter(17, league.getWxUrl())
		.setParameter(18, league.getSortValue())
		.setParameter(19, league.getIsValid())
		.setParameter(20, league.getDescr())
		.setParameter(21, currTime)
		.setParameter(22, currTime);
		
		try{
			int result = sqlUpdate.execute();
			league = findAllFromDBById(db, league.getId());
			// 添加Redis
			league.toRedis(jedis);
			// 添加索引
			league.toEs(client);
			
			return result;
		}catch(Exception e){
			logger.error("Error persist League to toDb : ", e);
		}
		return 0;
	}
	
	//更新League
	public static int update( Database db, Jedis jedis, Client client, League league, Date currTime ){
		if(league.getId()==null){
			return 0;
		}
		EbeanServer es = db.getDbByPartitionKey("CAREERX", "");
		StringBuffer sqlStr = new StringBuffer();
		sqlStr.append("UPDATE CX_LEAGUE SET ");
		sqlStr.append(" NAME= :name,");
		sqlStr.append(" ABBR= :abbr,");
		if(!StringUtils.isNotBlank(league.getLogoKey())){
			sqlStr.append(" LOGO_KEY= :logoKey,");
		}
		sqlStr.append(" LOCATION_ID= :locationId,");
		sqlStr.append(" ADDRESS= :address,");
		sqlStr.append(" SCHOOL_ID= :schoolId,");
		sqlStr.append(" SCHOOL_NAME= :schoolName,");
		sqlStr.append(" INDUSTRY= :industry,");
		sqlStr.append(" SIZE= :size,");
		sqlStr.append(" ZIPCODE= :zipCode,");
		sqlStr.append(" TEL= :tel,");
		sqlStr.append(" FAX= :fax,");
		sqlStr.append(" EMAIL= :email,");
		sqlStr.append(" CONTACT= :contact,");
		sqlStr.append(" WX_URL= :wxUrl,");
		sqlStr.append(" DESCR= :descr,");
		sqlStr.append(" SORT_VALUE= :sortValue,");
		sqlStr.append(" UPDATE_AT= :updateAt");
		sqlStr.append(" where IS_VALID<>-1 AND ID=:id");
		SqlUpdate sqlUpdate = es.createSqlUpdate(sqlStr.toString())
				.setParameter("name", league.getName())
				.setParameter("abbr", league.getAbbr())
				.setParameter("logoKey", league.getLogoKey())
				.setParameter("locationId", league.getLocationId())
				.setParameter("address", league.getAddress())
				.setParameter("schoolId", league.getSchoolId())
				.setParameter("schoolName", league.getSchoolName())
				.setParameter("size", league.getSize())
				.setParameter("industry", league.getIndustry())
				.setParameter("zipCode", league.getZipCode())
				.setParameter("tel", league.getTel())
				.setParameter("fax", league.getFax())
				.setParameter("email", league.getEmail())
				.setParameter("contact", league.getContact())
				.setParameter("wxUrl", league.getWxUrl())
				.setParameter("descr", league.getDescr())
				.setParameter("sortValue", league.getSortValue())
				.setParameter("updateAt", currTime)
				.setParameter("id", league.getId());
		try{
			int result = sqlUpdate.execute();
			// 添加Redis
			league = findAllFromDBById(db, league.getId());
			league.toRedis(jedis);
			// 添加索引
			league.toEs(client);
			
			return result;
		}catch(Exception e){
			logger.error("Error persist League to update : ", e);
		}
		return 0;
	}
	
	//更新职位数
	public static int updatePostCount(Database db, Jedis jedis, Client client, String id, int postCount) {
		if (id == null) {
			return 0;
		}
		EbeanServer es = db.getDbByPartitionKey("CAREERX", "");
		StringBuffer sqlStr = new StringBuffer();
		sqlStr.append("UPDATE CX_LEAGUE SET POST_COUNT= POST_COUNT+:postCount WHERE ID=:id");
		SqlUpdate sqlUpdate = es.createSqlUpdate(sqlStr.toString())
				.setParameter("postCount", postCount)
				.setParameter("id", id);

		try {
			int result = sqlUpdate.execute();
			League league = findFromDBById(db, jedis, id);
			// 添加Redis
			league.toRedis(jedis);
			// 添加索引
			league.toEs(client);
			return result;
		} catch (Exception e) {
			logger.error("Error persist Org to updatePostCount : ", e);
		}
		return 0;
	}
	
	// 根据ID判断数据库中是否存在
	public static boolean IsExistFromDBById(Database db, Jedis jedis, String id) {
		EbeanServer es = db.getDbByPartitionKey("CAREERX", "");
		StringBuffer sqlStr = new StringBuffer();
		sqlStr.append("SELECT * FROM CX_LEAGUE WHERE ID=:id");
		SqlQuery sqlQuery = es.createSqlQuery(sqlStr.toString()).setParameter("id", id);
		try {
			SqlRow sqlRow = sqlQuery.findUnique();
			if (sqlRow == null || sqlRow.size()==0) {
				return false;
			}
			return true;
		} catch (Exception e) {
			logger.error("Error persist Org to IsExistFromDBById : ", e);
		}
		return false;
	}
	
	//根据ID删除职位信息
	public static int deleteById( Database db, Jedis jedis, Client client, String id ){
		if(id==null || id.trim().equals("") ||  IsExistFromDBById(db, jedis, id)==false){
			return 0;
		}
		int result = 0;
		try {
			StringBuffer sqlStr = new StringBuffer();
			sqlStr.append("UPDATE CX_LEAGUE SET IS_VALID=-1 WHERE IS_VALID<>-1 AND ID=?");
			EbeanServer es = db.getDbByPartitionKey("CAREERX", id);
			SqlUpdate sqlUpdate = es.createSqlUpdate(sqlStr.toString())
					.setParameter(1, id);
			result = sqlUpdate.execute();
			
			League league = findAllFromDBById(db, id);
			// 添加Redis
			league.toRedis(jedis);
			// 添加索引
			league.toEs(client);
			
		} catch (Exception e) {
			logger.error("Error persist League to deleteLeagueById : ", e);
		}
		return result;
	}
	
	public static int deleteByIds( Database db, Jedis jedis, Client client, String[] idList ){
		int result = 0;
		for( String leagueId : idList ){
			deleteById( db, jedis, client, leagueId );
			result ++;
		}
		return result;
	}
	
}
